package com.ccut;

import com.ccut.Constant.ShapeType;

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;

public class Parallelogram extends MyShape {
    private double sideA;
    private double sideB;
    private double angle; // 角度制
    private ShapeType shapeType;

    public Parallelogram(double sideA, double sideB, double angle) {
        this.sideA = sideA;
        this.sideB = sideB;
        this.angle = angle;
        determineShapeType();
        setPerimeter(calculatePerimeter());
        setArea(calculateArea());
        addShape(this);
    }

    //矩形或菱形
    public Parallelogram(double side, double angle) {
        this(side, side, angle);
    }

    private void determineShapeType() {
        if (Math.abs(sideA - sideB) < 0.01) {
            if (Math.abs(angle - 90.0) < 0.01) {
                shapeType = ShapeType.SQUARE;
            } else {
                shapeType = ShapeType.RHOMBUS;
            }
        } else {
            if (Math.abs(angle - 90.0) < 0.01) {
                shapeType = ShapeType.RECTANGLE;
            } else {
                shapeType = ShapeType.PARALLELOGRAM;
            }
        }
    }

    public ShapeType getShapeType() {
        return shapeType;
    }

    public double calculatePerimeter() {
        return 2 * (sideA + sideB);
    }

    public double getPerimeter() {
        return calculatePerimeter();
    }

    public double calculateArea() {
        double radians = Math.toRadians(angle);
        return sideA * sideB * Math.sin(radians);
    }

    public double getArea() {
        return calculateArea();
    }

    @Override
    public String toString() {
        return "图形的类型:"+ getShapeType().getChineseName() + "\n" +
                "图形的边长分别为:"+ sideA + ", " + sideB + "\n" +
                "图形一组相邻两边的角度:"+ angle + "\n" +
                "图形的周长(近似):"+ String.format("%.2f", getPerimeter()) + "\n" +
                "图形的面积(近似):"+ String.format("%.2f", getArea());
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int width = getWidth();
        int height = getHeight();

        int centerX = width / 2;
        int centerY = height / 2;

        double radians = Math.toRadians(angle);
        int x1 = centerX - (int)(sideA * 10 / 2);
        int y1 = centerY;
        int x2 = x1 + (int)(sideA * 10);
        int y2 = y1;
        int x3 = x2 + (int)(sideB * 10 * Math.cos(radians));
        int y3 = y2 - (int)(sideB * 10 * Math.sin(radians));
        int x4 = x1 + (int)(sideB * 10 * Math.cos(radians));
        int y4 = y1 - (int)(sideB * 10 * Math.sin(radians));

        int[] xPoints = {x1, x2, x3, x4};
        int[] yPoints = {y1, y2, y3, y4};

        g2d.setColor(java.awt.Color.BLUE);
        g2d.fillPolygon(new Polygon(xPoints, yPoints, 4));
        g2d.setColor(java.awt.Color.BLACK);
        g2d.drawPolygon(new Polygon(xPoints, yPoints, 4));

        g2d.drawString("Shape: " + shapeType.name(), 10, 20);
        g2d.drawString("Perimeter: " + String.format("%.2f", getPerimeter()), 10, 40);
        g2d.drawString("Area: " + String.format("%.2f", getArea()), 10, 60);

        g2d.dispose();
    }

    public void showShape() {
        javax.swing.SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Shape Display");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(this);
            frame.setSize(400, 400);
            frame.setVisible(true);
        });
    }
}
